﻿using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Articles;
using Ue.Qcdz.Domain.Tags;
namespace Ue.Qcdz.UI.Controls
{
    /// <summary>
    /// 文章编辑控件
    /// </summary>
    public class ArticleEdit : SkinnedEntityEdit<Article>
    {
        private HiddenField hdfCategory;
        private HiddenField hdfTagKey;
        private TextBox tbxTitle;
        private TextBox tbxSubTitle;
        private TextBox tbxReference;
        private TextBox tbxKeyword;
        private TextBox tbxSummary;
        private TextBox tbxImageUrl;
        private FCKeditor tbxContent;
        private TextBox tbxDisplayOrder;
        private RadioButtonList rBtnListstatus;
        private DropDownList ddlTitleColor;
        private DropDownList ddlIsStrong;
        private RadioButtonList rBtnListIsTop;
        private TextBox tbxEditor;
        private FCKeditor tbxRelevance;
        private RadioButtonList rBtnListIsFocus;

        public ArticleEdit()
        {
            this.ReturnUrl = Domain.SiteUrls.Instance.ArticleManage();
        }

        protected override Article Enttiy
        {
            get
            {
                if (this.Action == "Delete")
                {
                    if (Context.Request.UrlReferrer != null)
                        this.ReturnUrl = Context.Request.UrlReferrer.ToString();
                    return new Article(this.EntityKey);
                }
                Tag tag = null;

                //分类：
                if (!string.IsNullOrEmpty(hdfCategory.Value))
                {
                    Domain.TreeViews.TreeNode node = ArticleService.TagTreeView.FindNodeBy(hdfCategory.Value);
                    tag = TagService.GetTag(node.Tag.Key);
                }
                else
                {
                    tag = TagService.GetTag(hdfTagKey.Value);
                }
                Article model = ArticleService.GetArticle(this.EntityKey);
                if (model == null)
                    model = new Article(this.EntityKey);

                model.Tag = tag;
                if (tbxTitle != null)
                    model.Title = tbxTitle.GetValue<string>();
                if (tbxSubTitle != null)
                    model.SubTitle = tbxSubTitle.GetValue<string>();
                if (tbxSummary != null)
                    model.Summary = tbxSummary.GetValue<string>();
                if (tbxReference != null)
                    model.Reference = tbxReference.GetValue<string>();
                if (tbxKeyword != null)
                    model.Keyword = tbxKeyword.GetValue<string>();
                if (tbxImageUrl != null)
                    model.ImageUrl = tbxImageUrl.GetValue<string>();
                if (tbxContent != null)
                    model.Content = tbxContent.Value;
                if (tbxDisplayOrder != null)
                    model.DisplayOrder = tbxDisplayOrder.GetValue<int>();
                if (rBtnListstatus != null)
                    model.Status = rBtnListstatus.SelectedValue == "0" ? 0 : 1;
                if (ddlTitleColor != null)
                    model.TitleColor = ddlTitleColor.SelectedValue;
                if (ddlIsStrong != null)
                    model.IsStrong = ddlIsStrong.SelectedValue == "1" ? true : false;
                if (rBtnListIsTop != null)
                    model.IsTop = rBtnListIsTop.SelectedValue == "1" ? 1 : 0;
                if (tbxEditor != null)
                    model.Editor = tbxEditor.GetValue<string>();
                if (tbxRelevance != null)
                    model.Relevance = tbxRelevance.Value;
                if (rBtnListIsFocus != null)
                    model.IsFocus = rBtnListIsFocus.SelectedValue == "1" ? 1 : 0;
                return model;
            }
            set
            {

                if (value == null)
                    return;
                tbxTitle.SetValue(value.Title);
                tbxSubTitle.SetValue(value.SubTitle);
                tbxSummary.SetValue(value.Summary);
                tbxReference.SetValue(value.Reference);
                tbxKeyword.SetValue(value.Keyword);
                tbxImageUrl.SetValue(value.ImageUrl);
                tbxContent.Value = value.Content;
                tbxDisplayOrder.SetValue(value.DisplayOrder);
                tbxEditor.SetValue(value.Editor);
                tbxRelevance.Value = value.Relevance;
                //
                rBtnListstatus.SelectedIndex = -1;
                rBtnListstatus.Items.FindByValue(value.Status.ToString()).Selected = true;
                //
                if (ddlTitleColor.Items.FindByValue(value.TitleColor) != null)
                {
                    ddlTitleColor.SelectedIndex = -1;
                    ddlTitleColor.Items.FindByValue(value.TitleColor).Selected = true;
                }
                if (ddlIsStrong.Items.FindByValue(value.IsStrong == true ? "1" : "0") != null)
                {
                    ddlIsStrong.SelectedIndex = -1;
                    ddlIsStrong.Items.FindByValue(value.IsStrong == true ? "1" : "0").Selected = true;
                }
                //
                rBtnListIsTop.SelectedIndex = -1;
                rBtnListIsTop.Items.FindByValue(value.IsTop.ToString()).Selected = true;
                //
                rBtnListIsFocus.SelectedIndex = -1;
                rBtnListIsFocus.Items.FindByValue(value.IsFocus.ToString()).Selected = true;
                //
                this.hdfTagKey.Value = value.Tag.Key.ToString();
            }
        }

        protected override void InitSkin(System.Web.UI.Control skin)
        {

            hdfCategory = skin.FindControl<HiddenField>("hdfCategory");
            hdfTagKey = skin.FindControl<HiddenField>("hdfTagKey");
            ddlTitleColor = skin.FindControl<DropDownList>("ddlTitleColor");
            tbxTitle = skin.FindControl<TextBox>("tbxTitle");
            tbxSubTitle = skin.FindControl<TextBox>("tbxSubTitle");
            tbxReference = skin.FindControl<TextBox>("tbxReference");
            tbxKeyword = skin.FindControl<TextBox>("tbxKeyword");
            tbxSummary = skin.FindControl<TextBox>("tbxSummary");
            tbxImageUrl = skin.FindControl<TextBox>("tbxImageUrl");
            tbxContent = skin.FindControl<FCKeditor>("tbxContent");
            tbxDisplayOrder = skin.FindControl<TextBox>("tbxDisplayOrder");
            rBtnListstatus = skin.FindControl<RadioButtonList>("rBtnListstatus");
            ddlIsStrong = skin.FindControl<DropDownList>("ddlIsStrong");
            rBtnListIsTop = skin.FindControl<RadioButtonList>("rBtnListIsTop");
            tbxEditor = skin.FindControl<TextBox>("tbxEditor");
            tbxRelevance = skin.FindControl<FCKeditor>("tbxRelevance");
            rBtnListIsFocus = skin.FindControl<RadioButtonList>("rBtnListIsFocus");

        }

        protected override string GetSkinFileName()
        {
            return "~/Admin/Themes/Skins/Articles/ArticleEdit.ascx";
        }
    }
}